home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 10 / Example 10.1 / building.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-06-30  |  3.7 KB  |  161 lines

  1. #include "building.h"
  2.  
  3. std::vector<MESH*> buildingMeshes;
  4.  
  5. void LoadBuildingResources(IDirect3DDevice9* Device)
  6. {
  7.     std::vector<std::string> fnames;
  8.  
  9.     fnames.push_back("meshes/townhall.x");
  10.     fnames.push_back("meshes/barracks.x");
  11.     fnames.push_back("meshes/tower.x");
  12.  
  13.     for(int i=0;i<fnames.size();i++)
  14.         buildingMeshes.push_back(new MESH((char*)fnames[i].c_str(), Device));
  15. }
  16.  
  17. void UnloadBuildingResources()
  18. {
  19.     for(int i=0;i<buildingMeshes.size();i++)
  20.         if(buildingMeshes[i] != NULL)
  21.             delete buildingMeshes[i];
  22.  
  23.     buildingMeshes.clear();
  24. }
  25.  
  26. bool PlaceOk(int buildType, INTPOINT mp, TERRAIN *terrain)
  27. {
  28.     if(terrain == NULL)return false;
  29.  
  30.     BUILDING b(buildType, 0, mp, NULL, false, NULL);
  31.     RECT r = b.GetMapRect(1);
  32.  
  33.     for(int y=r.top;y<=r.bottom;y++)
  34.         for(int x=r.left;x<=r.right;x++)
  35.         {
  36.             //Building must be within map borders
  37.             if(!terrain->Within(INTPOINT(x,y)))return false;
  38.             MAPTILE *tile = terrain->GetTile(x, y);
  39.             if(tile == NULL)return false;
  40.  
  41.             //The terrain must be level and walkable
  42.             if(tile->m_height != 0.0f || !tile->m_walkable)return false;
  43.         }
  44.  
  45.     return true;
  46. }
  47.  
  48. //////////////////////////////////////////////////////////////////////////////////
  49. //                                BUILDING                                        //
  50. //////////////////////////////////////////////////////////////////////////////////
  51.  
  52. BUILDING::BUILDING(int _type, int _team, INTPOINT mp, TERRAIN *_terrain, bool _affectTerrain, IDirect3DDevice9* Dev)
  53. {
  54.     m_type = _type;
  55.     m_team = _team;
  56.     m_mappos = mp;
  57.     m_pTerrain = _terrain;
  58.     m_affectTerrain = _affectTerrain;
  59.     m_pDevice = Dev;    
  60.     m_team = 0;
  61.     m_range = m_damage = 0;
  62.     m_meshInstance.SetMesh(buildingMeshes[m_type]);
  63.     m_isBuilding = true;
  64.  
  65.     if(m_type == 0)        //Townhall
  66.     {
  67.         m_hp = m_hpMax = 600;
  68.         m_sightRadius = 12;
  69.         m_name = "Townhall";
  70.         m_mapsize.Set(4,2);
  71.         m_meshInstance.SetScale(D3DXVECTOR3(0.13f, 0.13f, 0.13f));
  72.     }    
  73.     else if(m_type == 1)        //Barracks
  74.     {
  75.         m_hp = m_hpMax = 450;
  76.         m_sightRadius = 13;
  77.         m_name = "Barracks";
  78.         m_mapsize.Set(2,4);
  79.         m_meshInstance.SetScale(D3DXVECTOR3(0.15f, 0.15f, 0.15f));
  80.     }    
  81.     else if(m_type == 2)        //Tower
  82.     {
  83.         m_hp = m_hpMax = 750;
  84.         m_sightRadius = 20;
  85.         m_name = "Tower";
  86.         m_mapsize.Set(2,2);
  87.         m_meshInstance.SetScale(D3DXVECTOR3(0.13f, 0.13f, 0.13f));
  88.     }
  89.  
  90.     if(m_pTerrain != NULL)
  91.         m_position = m_pTerrain->GetWorldPos(m_mappos) + D3DXVECTOR3(m_mapsize.x / 2.0f, 0.0f, -m_mapsize.y / 2.0f);
  92.     else m_position = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  93.  
  94.     m_meshInstance.SetPosition(m_position);
  95.  
  96.     m_BBox = m_meshInstance.GetBoundingBox();
  97.     m_BBox.max -= D3DXVECTOR3(0.2f, 0.2f, 0.2f);
  98.     m_BBox.min += D3DXVECTOR3(0.2f, 0.2f, 0.2f);
  99.  
  100.     //Update the tiles of the terrain
  101.     if(m_pTerrain != NULL && m_affectTerrain)
  102.     {
  103.         RECT mr = GetMapRect(0);
  104.  
  105.         for(int y=mr.top;y<=mr.bottom;y++)
  106.             for(int x=mr.left;x<=mr.right;x++)
  107.             {
  108.                 MAPTILE *tile = m_pTerrain->GetTile(x, y);
  109.                 if(tile != NULL)
  110.                 {
  111.                     tile->m_walkable = false;
  112.                     tile->m_pMapObject = this;
  113.                 }
  114.             }
  115.  
  116.         m_pTerrain->UpdatePathfinding(&GetMapRect(1));
  117.     }
  118. }
  119.  
  120. BUILDING::~BUILDING()
  121. {
  122.     //restore the tiles of the terrain
  123.     if(m_pTerrain != NULL && m_affectTerrain)
  124.     {
  125.         RECT mr = GetMapRect(0);
  126.  
  127.         for(int y=mr.top;y<=mr.bottom;y++)
  128.             for(int x=mr.left;x<=mr.right;x++)
  129.             {
  130.                 MAPTILE *tile = m_pTerrain->GetTile(x, y);
  131.                 if(tile != NULL)
  132.                 {
  133.                     tile->m_walkable = true;
  134.                     tile->m_pMapObject = NULL;
  135.                 }
  136.             }
  137.  
  138.         m_pTerrain->UpdatePathfinding(&GetMapRect(1));
  139.     }
  140. }
  141.  
  142. void BUILDING::Render()
  143. {
  144.     if(m_visible)
  145.         m_meshInstance.Render();
  146. }
  147.  
  148. void BUILDING::Update(float deltaTime)
  149. {
  150.     //Train units, upgrade things etc here...
  151. }
  152.  
  153. BBOX BUILDING::GetBoundingBox()
  154. {
  155.     return m_BBox;
  156. }
  157.  
  158. D3DXMATRIX BUILDING::GetWorldMatrix()
  159. {
  160.     return m_meshInstance.GetWorldMatrix();
  161. }